﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 海漫思考核.BLL;
using 海漫思考核.DAL;
using System.IO;

namespace 海漫思考核
{
    public partial class AddNove : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["User"] != null)
                {
                    //给前端类型下拉框赋值
                    var types = NoveBLL.SelectAllNoveType();
                    foreach (NoveType item in types)
                    {
                        ddl_type.Items.Add(item.Tname);
                        ddl_type.Items[item.Tid - 1].Value = item.Tid.ToString();
                    }
                    //默认为当前日期
                    text_uptime.Value = DateTime.Now.ToString("yyyy-MM-dd");
                }
                else
                {
                    Response.Write("<script>alert('非法登录!即将返回登录页面!');location.href = 'Login.aspx';</script>");
                }
            }
        }
        /// <summary>
        /// 新增点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_add_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)//判断文件是否存在
            {
                string path = FileUpload1.FileName;//获取文件路径
                string filetype = path.Substring(path.LastIndexOf(".")).ToLower();//获取文件格式
                if (filetype == ".jpg" || filetype == ".png")//判断文件格式是否正确
                {
                    if (NoveBLL.SelectNoveByName(text_name.Value) == null)//判断小说是否已存在
                    {
                        NoveInfo nove = new NoveInfo()//创建小说对象
                        {
                            Nname = text_name.Value,
                            Nauthor = text_author.Value,
                            Nchapter = int.Parse(text_chapter.Value),
                            Nimgpath = $"image/{text_name.Value + filetype}",
                            Ncontentpath = $"text/{text_name.Value}/",
                            Nuptime = Convert.ToDateTime(text_uptime.Value),
                            Ntype = int.Parse(ddl_type.SelectedValue),
                            Nstate = int.Parse(ddl_state.SelectedValue)
                        };
                        if (NoveBLL.NoveAdd(nove))//添加小说对象
                        {
                            //上传小说封面和创建小说存储路径
                            Directory.CreateDirectory(Server.MapPath("text/") + text_name.Value);
                            FileUpload1.SaveAs(Server.MapPath("image/" + text_name.Value) + filetype);
                            Response.Write("<script>alert('新增成功!');location.href = 'ManageNove.aspx'</script>");
                        }
                        else
                            Response.Write("<script>alert('新增失败!')</script>");
                    }
                    else
                        Response.Write("<script>alert('小说已存在!')</script>");
                }
                else
                    Response.Write("<script>alert('图片格式不支持!')</script>");
            }
        }
        /// <summary>
        /// 返回点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_back_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManageNove.aspx");
        }
    }
}